package com.ljw.vendingmachine.service;

import android.nfc.Tag;
import android.util.Log;

import com.ljw.vendingmachine.App;
import com.ljw.vendingmachine.bean.MessageEvent;
import com.ljw.vendingmachine.utils.Constant;
import com.ljw.vendingmachine.utils.LogUtil;
import com.ljw.vendingmachine.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

public class EchoWebSocketListener extends WebSocketListener {
    // private WebSocket mSocket;

    String TAG = "EchoWebSocketListener";




    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        LogUtil.d("EchoWebSocketListener"," websocket连接成功！");
     //   mSocket = webSocket;
     //   mSocket.send(Constant.COMD_LOGIN);
      //  mSocket.send(Constant.COMD_GET_MAIN_GOODS);




    }

    @Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {
        LogUtil.d("EchoWebSocketListener","onMessage<ByteString>");

    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {
        LogUtil.d("EchoWebSocketListener","onMessage<String>");
        LogUtil.e( TAG + "rockey onMessage",text);
        try {
            JSONObject jsonObject=new JSONObject(text);
            String cmd = jsonObject.getString("cmdCode");

            //返回首页商品列表信息
            if ("0100".equals(cmd)){
                String data = jsonObject.getString("data");
                SPUtils.put(App.getApp(),Constant.PASSWORD,data);
            }
            //返回首页商品列表信息
            if ("0201".equals(cmd)){
                JSONObject data = jsonObject.getJSONObject("data");
                MessageEvent messageEvent=new MessageEvent();
                messageEvent.setData(data);
                messageEvent.setCode(Constant.EVENTBUS_CODE_MAIN_GOODS_LIST);
                EventBus.getDefault().postSticky(messageEvent);
            }
            //返回首页商品列表信息
            if ("0202".equals(cmd)){//
                JSONArray data = jsonObject.getJSONArray("data");
                MessageEvent messageEvent=new MessageEvent();
                messageEvent.setData(data);
                messageEvent.setCode(Constant.EVENTBUS_CODE_MORE_GOODS_LIST);
                EventBus.getDefault().post(messageEvent);
            }
            if ("0203".equals(cmd)){//支付二维码信息
                JSONObject data = jsonObject.getJSONObject("data");
                MessageEvent messageEvent=new MessageEvent();
                messageEvent.setData(data);
                messageEvent.setCode(Constant.EVENTBUS_CODE_QRCODE);
                EventBus.getDefault().post(messageEvent);
            }
            if ("0205".equals(cmd)){//支付完成
                String data = jsonObject.getString("data");
              //  String ids = data.getString("ids");
                MessageEvent messageEvent=new MessageEvent();
                messageEvent.setData(data);
                messageEvent.setCode(Constant.EVENTBUS_CODE_PAY_SECCESS);
                EventBus.getDefault().post(messageEvent);
            }
            //4.4、	修改机器格子状态
            if ("0302".equals(cmd)){
              //  String data = jsonObject.getString("data");
                MessageEvent messageEvent=new MessageEvent();

                messageEvent.setData(jsonObject);
                messageEvent.setCode(Constant.EVENTBUS_CODE_SINGLE_GOOD_TEST_RESULT);
                EventBus.getDefault().postSticky(messageEvent);
            }
            //4.7、	获取机器格子信息
           if ("0305".equals(cmd)){
               JSONArray data = jsonObject.getJSONArray("data");
                MessageEvent messageEvent=new MessageEvent();
                messageEvent.setData(data);
                messageEvent.setCode(Constant.EVENTBUS_CODE_ALL_GOOD_LIST);
                EventBus.getDefault().post(messageEvent);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        LogUtil.d("EchoWebSocketListener","onClosed");
    }

    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        LogUtil.d("EchoWebSocketListener","onClosing");
    }

    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        LogUtil.d("EchoWebSocketListener","onFailure");
        t.printStackTrace();
    }



}



